//
//  WFNavigationView.m
//  谷居
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 刘万方. All rights reserved.
//


#import "WFNavigationView.h"
#import "Masonry.h"
#import "WFDrop_DownView.h"
#import "WFPicsTitleModel.h"
#import "WFGuideTitleModel.h"
#import "WFCaseTitleModel.h"
@interface WFNavigationView ()<wfDrop_DownViewDelegate>

@property (nonatomic, assign) NSInteger buttonIndex;

@property (nonatomic, retain) NSMutableArray *caseTitleArray; /** 存储案例导航栏的数组. */

@property (nonatomic, retain) NSMutableArray *picTitleArray; /** 用来存储图库导航栏的数组. */

@property (nonatomic, retain) NSMutableArray *guideTitleArray; /** 用来存储攻略导航栏的数组. */

@property (nonatomic, retain) NSMutableArray *buttonStateArray; /** 用来存储button点击状态的数组. */

@property (nonatomic, retain) WFDrop_DownView *drop_downView; /** 创建下拉View. */

@property (nonatomic, assign) BOOL isCreated; /** Drop_downView是否创建. */

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, retain) NSMutableDictionary *picsDic; /** 图库字典. */

@property (nonatomic, retain) NSMutableDictionary *guideDic; /** 攻略字典. */

@end

@implementation WFNavigationView


- (void)createTitleView:(NSMutableArray *)titleArray {
    
    
    
    self.buttonStateArray = [NSMutableArray array];
    
    /** for 循环创建按钮. */
    if (self.buttonCount != 0) {
        for (int i = 0; i < self.buttonCount; i++) {
            
            
            /** 创建状态数组. */
            [self createStateArray];
            
            
            /** 每个按钮的X坐标. */
            self.buttonWidth = SCREEN_WIDTH / self.buttonCount;
            CGFloat x = i * self.buttonWidth;
            [self createButton:x index:i titleArray:titleArray]; /** titleArray */
        }

    }
}

- (void)createButton:(CGFloat)X index:(int)index titleArray:(NSMutableArray*)titleArray {
    _backgrounddView = [[WFUIView alloc] initWithFrame:CGRectMake(X, 0, self.buttonWidth, 20)];
    [self addSubview:_backgrounddView];
    [_backgrounddView release];
    
    WFImageView *myView = [[WFImageView alloc] init];
    [_backgrounddView addSubview:myView];
    [myView release];
    [myView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_backgrounddView);
//        make.left.equalTo(_backgrounddView).offset(10);
//        make.right.equalTo(_backgrounddView).offset(-10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(60);
    }];
    
    myView.userInteractionEnabled = YES;
    
    
    
    
    _button = [WFButtonText buttonWithType:UIButtonTypeCustom];
    [myView addSubview:_button];
    [_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(myView);
        make.top.equalTo(myView);
        make.height.mas_equalTo(20);
        make.width.greaterThanOrEqualTo(@20);
        
    }];
    
    [_button setTitle:titleArray[index] forState:UIControlStateNormal];
    _button.titleLabel.font = [UIFont systemFontOfSize:14];
    [_button sizeToFit];        
    [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button.tag = 1000 + index;
    [_button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    _butImageView = [[WFImageView alloc] init];
    [myView addSubview:_butImageView];
    [_butImageView release];
    _butImageView.tag = 2000 + index;
    [_butImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_backgrounddView);
        make.left.equalTo(_button.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(16, 16));
        
    }];

    _butImageView.image = [UIImage imageNamed:@"箭头向下.png"];
    
    
    
    
}


#pragma mark - 点击button
- (void)clickButton:(WFButtonText *)button {
    
    /** 打印被点击的button 的tag值. */

    /** 找到被点击的按钮. */
    /** 通过tag值来设置index. */
    NSInteger index = button.tag - 1000;
    /** 判断Drop_downView是否被创建出来. */
    [self isDrop_downViewCreatedWith:index];

    /** 直接加旋转. 不需改变状态值. */
    [self changeArrowDirectionWithIndex:index];

    
    /** 通过判断状态数组里的值来改变箭头方向*/
    [self changeButtonStateWithIndex:index];
    
    
}


#pragma mark - 创建状态数组的方法
- (void)createStateArray {
//    BOOL state = 0;
    NSString *state = @"0";
    [self.buttonStateArray addObject:state];
}


#pragma mrak - 改变点击状态的方法
- (void)changeButtonStateWithIndex:(NSInteger)index {
    /** 先判断index位置的状态, 如果没被点击, 将状态设置为1, 同时将其他位置的状态设置为0. 如果点击过了, 将其状态设置为0. */
    if ([self.buttonStateArray[index] isEqualToString:@"0"]) {
        for (int i = 0; i < self.buttonStateArray.count; i++) {
            self.buttonStateArray[i] = @"0";
        }
        [self.buttonStateArray replaceObjectAtIndex:index withObject:@"1"];
        
    } else {
        [self.buttonStateArray replaceObjectAtIndex:index withObject:@"0"];
    }
    
}

#pragma mark - 添加旋转
- (void)changeArrowDirectionWithIndex:(NSInteger)index {
    int i = 0;
    /** 当点击的地方的状态为1的时候, 直接旋转. */
    if ([self.buttonStateArray[index] isEqualToString:@"1"]) {
        [self makeRoalByIndex:index];
    
    } else {

        /** 当点击状态为0的时候, 旋转并遍历装态数组, 如果有其他位置状态为1的话, 将该位置的图片旋转. */
        [self makeRoalByIndex:index];
        for (NSString *state in self.buttonStateArray) {
            if ([state isEqualToString:@"1"] && index != i) {
                /** 获取当前状态为1的imageView, 并旋转. */
                [self makeRoalByIndex:i];
                
                return;
            }
            i++;
        }
    }
    
}

#warning 旋转恢复
#pragma mark - 旋转方法, 旋转后会恢复位置
- (void)makeRoalByIndex:(NSInteger)index {
    /** 获取当前需要旋转的图片. */
    WFImageView *buttonImageView = [self.superview viewWithTag:2000 + index];
    
    /** 添加旋转动画. */
    CABasicAnimation *buttonRoal = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    /** 设置旋转起点. */
    buttonRoal.fromValue = @0;
    /** 设置旋转终点. */
    buttonRoal.toValue = [NSNumber numberWithFloat:M_PI ];
    /** 设置旋转时长. */
    buttonRoal.duration = 0.5;
    /** 设置重复次数. */
//    buttonRoal.repeatCount = 0;
    
//    * 设置结束后是否回逆向到原始位置. 
    buttonRoal.autoreverses = NO;
    
    /** 旋转之后是否按照结束位置继续旋转. */
    buttonRoal.cumulative = YES;
    
    /** 将动画添加到buttonImageView上. */
    [buttonImageView.layer addAnimation:buttonRoal forKey:@"basic"];
}

#pragma mark - 判断是否已经创建了drop_view
- (void)isDrop_downViewCreatedWith:(NSInteger)index {
    /** 根据stateARray 的状态判断是否已经创建了view.
     *  如果是由该按钮创建的,移除
     */
    
    if ([self.buttonStateArray containsObject:@"1"]) {
        
        for (int i = 0; i < self.buttonStateArray.count; i++) {
                if ([self.buttonStateArray[i] isEqualToString:@"1"] && i == index) {
                    self.isCreated = 1;
                    [self removeDropVeiw];
                    
#pragma mark - break 当发现点击的index == i时候,break 保存isCreate的值.
                    break;
                } else {
                    self.isCreated = 0;
                    [_drop_downView removeCollectionView];
                }
        }
            } else {
        [self createDrop_dowmView];
//        [self sendDataToDropView:index];
        self.isCreated = 0;
    }
    
#pragma mark - 通过isCreated的值来判断是否向Drop_downView传值
    if (!self.isCreated ) {
        [self sendDataToDropView:index];
    }

}

#pragma mark - 将数据传输到_Drop_downView中 (只有第一个界面.需要完善..)
- (void)sendDataToDropView:(NSInteger)index {
    
    NSMutableArray *array = [NSMutableArray array];
    switch (self.navigationType) {
        case 0:
           array = [self createCaseArrayWithIndex:index];
            break;
        case 1:
            array = [self createPicsArrayWithIndex:index];
            break;
            case 2:
            array = [self createGuideArrayWithIndex:index];
            break;
        default:
            break;
    }
    
    [_drop_downView createCollectionViewWithArray:array andNavigationType:self.navigationType index:self.index];
    

}


#pragma mark - 创建每个界面地的数据

- (NSMutableArray *)createCaseArrayWithIndex:(NSInteger)index {
    NSMutableArray *arr = [NSMutableArray array];
    NSMutableArray *array = [NSMutableArray array];

    switch (index) {
        case 0:
            array = self.titleDic[@"area"];
            arr = [WFCaseTitleModel baseModelByArray:array];
            break;
        case 1:
            array = self.titleDic[@"budget"];
            arr = [WFCaseTitleModel baseModelByArray:array];

            break;
        case 2:
            array = self.titleDic[@"type"];
            arr = [WFCaseTitleModel baseModelByArray:array];

            break;
        case 3:
            array = self.titleDic[@"style"];
            arr = [WFCaseTitleModel baseModelByArray:array];

            break;
        default:
            break;
    }
    self.index = index;
    return arr;

}

- (NSMutableArray *)createPicsArrayWithIndex:(NSInteger)index {
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *arr = [NSMutableArray array];
    switch (index) {
        case 0:
            arr = self.picsDic[@"space"];
            array = [WFPicsTitleModel baseModelByArray:arr];
            break;
            case 1:
            arr = self.picsDic[@"style"];
            array = [WFPicsTitleModel baseModelByArray:arr];
            break;
            case 2:
            arr = self.picsDic[@"color"];
            array = [WFPicsTitleModel baseModelByArray:arr];
            break;
        default:
            break;
    }
    self.index = index;
    return array;
}
- (NSMutableArray *)createGuideArrayWithIndex:(NSInteger)index {
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *arr2 = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0", @"id", @"不限", @"name", nil];

    switch (index) {
        case 0:
            arr2 = ((NSMutableArray *)self.guideDic[@"before"]).mutableCopy;
            [arr2 insertObject:dic atIndex:0];
            NSLog(@"arrclass: %@", arr2.class);
            NSLog(@"arrayclase: %@", array.class);
            array = [WFGuideTitleModel baseModelByArray:arr2];
            break;
        case 1:
            arr2 = ((NSMutableArray *)self.guideDic[@"in"]).mutableCopy;
            [arr2 insertObject:dic atIndex:0];
            array = [WFGuideTitleModel baseModelByArray:arr2];
            break;
            case 2:
            arr2 = ((NSMutableArray *)self.guideDic[@"after"]).mutableCopy;
            [arr2 insertObject:dic atIndex:0];
            array = [WFGuideTitleModel baseModelByArray:arr2];
            break;
        default:
            break;
    }

    self.index = index;
    return array;
}

#pragma mark - 创建底层Drop_DownView
/** 创建View.. */
- (void)createDrop_dowmView{
    _drop_downView = [[WFDrop_DownView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 84 - 48)];
    _drop_downView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self.superview addSubview:_drop_downView];
    [_drop_downView release];
    
    _drop_downView.delegate = self;
}

#pragma mark - Drop_View的协议方法.
/** 点击Drop_DownView方法. */
/** 这个方法移除DropView的同时改变了状态数组. 再次低级的时候就不会因为再次移除view而崩溃. */
- (void)clickDrop_downView {
    [self removeDropVeiw];
    /** 将状态数组全部重置为0 */
    
    /** 获取当前的状态数组, 判断哪个是旋转过得. */
    for (int i = 0; i < self.buttonStateArray.count; i++) {
        if ([self.buttonStateArray[i] isEqualToString:@"1"]) {
            [self makeRoalByIndex:i];
        }
    }
    
    
    [self.buttonStateArray removeAllObjects];
    self.buttonStateArray = @[@"0", @"0", @"0", @"0", @"0"].mutableCopy;
    
}

/** 将标题带到这个界面. */
- (void)takeTitleButtonName:(NSString *)name andId:(NSString *)ID{
    UIButton *button = [self viewWithTag:1000 + self.index];

    if ([name isEqualToString:@"不限"]) {
        [button setTitle:self.caseTitleArray[self.index] forState:UIControlStateNormal];
        
    } else {
        [button setTitle:name forState:UIControlStateNormal];
    }
    
    /** 移除view的同时一定要将状态数组的状态相应改变. */
    [self clickDrop_downView];
    
#pragma mark - 通过id再次刷新页面. 
    [self.delegate backTorefreshTableViewWithId:ID indexType:self.index];
}

/** 移除View. */

- (void)removeDropVeiw {
    
    [_drop_downView removeFromSuperview];
}
#pragma mark - 通过type的setter方法通知需要哪个页面的navigation
- (void)setNavigationType:(NavigationType)navigationType {
    
    self.caseTitleArray = @[@"面积", @"预算", @"户型", @"风格"].mutableCopy;
    self.picTitleArray = @[@"空间", @"风格", @"色系"].mutableCopy;
    
    self.guideTitleArray = @[@"装修前", @"装修中", @"装修后"].mutableCopy;


    
    _navigationType = navigationType;
    
    switch (_navigationType) {
        case 0:
            /** 创建数据. (plist文件)*/
            [self createCaseTypeData];
            break;
        case 1:
            [self createPicsTypeData];
            break;
            case 2:
            [self createGuideTypeData];
            break;
        default:
            break;
    }
}


- (void)createCaseTypeData {
    NSString *strPath = [[NSBundle mainBundle] pathForResource:@"case" ofType:@"plist"];
    self.titleDic = [NSMutableDictionary dictionaryWithContentsOfFile:strPath];
    [self setButtonTitle:self.caseTitleArray];
}

- (void)createPicsTypeData {
    self.picsDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"picsTitleDic"];
    [self setButtonTitle:self.picTitleArray];
    
}

- (void)createGuideTypeData {
    self.guideDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"guideTitleDic"];
//    NSLog(@"%@", self.guideDic);
    [self setButtonTitle:self.guideTitleArray];
}


- (void)setButtonTitle:(NSMutableArray *)titleArray {
    [self createTitleView:titleArray];

}


#pragma - mark 懒加载
- (NSMutableDictionary *)picsDic {
    if (_picsDic == nil) {
        _picsDic = [[NSMutableDictionary dictionary]retain];
    }
    return _picsDic;
}



- (void)dealloc {
//    [_buttonStateArray release];
    [_caseTitleArray release];
    [_guideTitleArray release];
    [_picTitleArray release];
    [super dealloc];
}

@end
